/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			pgprintparams.cc
  \date			Jun 2012
  \author		TNick

  \brief		Contains the implementation of PgPrintParams class


*//*


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Please read COPYING and README files in root folder
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<cpg/cpg.h>

#include	<QPainter>
#include	<QFileDialog>

#include	<cpg/printing/pgprintmanager.h>
#include	<cpg/logic/pgspace.h>

#include	"pgprintparams.h"


/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DEFINITIONS    --------------------------------------------------------- */

using namespace cpg;

/*  DEFINITIONS    ========================================================= */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */

/*  DATA    ================================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

/* ------------------------------------------------------------------------- */
PrintParams::PrintParams	( PgSpace * space )
    : BBlocks::PrintParams()
{
	space_ = space;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
PrintParams::PrintParams	( PrintParams * src )
    : BBlocks::PrintParams( src )
{
    /* stub */
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
PrintParams::PrintParams	( void )
    : BBlocks::PrintParams()
{
    /* stub */
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
PrintParams::~PrintParams	( void )
{
	/* stub */
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool				PrintParams::print				(
		QPrinter * dest_p, QString & s_err )
{
	PrintManager::Printer p_base;
	PrintManager::printer( index_, p_base );
	bool b_file = false;

	switch ( p_base.ty_ )	{
	case PrintManager::BASIC:
		dest_p->setOutputFormat( QPrinter::NativeFormat );
		dest_p->setPrinterName( p_base.name_ );
		break;
	case PrintManager::BUILD_IN_PDF:
		dest_p->setOutputFormat( QPrinter::PdfFormat );
		b_file = true;
		break;
	case PrintManager::BUILD_IN_PS:
		dest_p->setOutputFormat( QPrinter::PostScriptFormat );
		b_file = true;
		break;
	case PrintManager::BUILD_IN_IMG:
		/** @todo implement image printing */
		b_file = true;
		break;
	case PrintManager::NOT_FOUND:
		s_err = QString( "The printer %1 was not found" )
				.arg( p_base.name_ );
		return false;
	};
	if ( b_file )
	{
		QString file_name = QFileDialog::getSaveFileName(
					CpgLibrary::activeWindow(),
					QObject::tr( "Select output file name and location:" ),
					QString(),
					"*.*"
					);
		if ( file_name.isEmpty() )
		{ /* not an error */
			return true;
		}
		dest_p->setOutputFileName( file_name );
	}

	dest_p->setPaperSize( paper_size_ );
	dest_p->setPaperSource( paper_src_ );
	if ( isPortrait() )
		dest_p->setOrientation( QPrinter::Portrait );
	else
		dest_p->setOrientation( QPrinter::Landscape );
	if ( isGrayScaled() )
		dest_p->setColorMode( QPrinter::GrayScale );
	else
		dest_p->setColorMode( QPrinter::Color );
	dest_p->setDuplex( duplexModeQt() );
	if ( isFirstPageFirst() )
		dest_p->setPageOrder( QPrinter::FirstPageFirst );
	else
		dest_p->setPageOrder( QPrinter::LastPageFirst );
	dest_p->setCollateCopies( isCollated() );
	dest_p->setPageMargins(
				margins_.left_,
				margins_.top_,
				margins_.right_,
				margins_.bottom_,
				units_
				);
	dest_p->setFullPage( isOriginPaper() );

	/*

	QList<int>				pages_;
	ABOX2D					u_rect_;

	Gui::PgDocGui *			doc_gui_;
	*/



	/* start to print */
	if ( dest_p->isValid() == false )
	{
		s_err = QString( "Printer is unusable for unknown reasons." );
		return false;
	}
	QPainter painter;
	if ( painter.begin( dest_p ) == false )
	{
		s_err = QString( "Failed to start printing (is file writable?)." );
		return false;
	}

	/* the content */
//	Gui::PgDocView * v = doc_gui_->activeView();
//	PgSpace * spc = v->space();
//	QRect rect = painter.viewport();
//	ABOX2D	ext;
//	spc->extents( ext );


//	painter.setBrush( QBrush( Qt::black ) );
//	painter.drawRect( rect );
//	switch ( mp_ )	{

//	case CRT_VIEW:	{
//		if ( v == NULL )
//		{
//			s_err = QString( "There is no current view to present" );
//			return false;
//		}
//		painter.translate( v->crtPosX(), v->crtPosY() );

//		/* we are trying to fit */
//		REALV scl_f;
//		REALV w_scl = (REALV)rect.width() / ext.width();
//		REALV h_scl = (REALV)rect.height() / ext.height();
//		if ( w_scl > h_scl )
//		{
//			scl_f = w_scl;
//			/** @todo implement */
//		}
//		else
//		{
//			scl_f = h_scl;
//			/** @todo implement */
//		}


////		REALV w_scl = (REALV)v->width() / (REALV)rect.width();
////		REALV h_scl = (REALV)v->height() / (REALV)rect.height();
////		/* center */
////		QRectF	r( v->crtPosX(), v->crtPosY(), scl_f*v->width(), scl_f*v->height() );
//		painter.scale( scl_f, scl_f );
//		QRectF	r;
//		spc->paint( painter, r );
//		break; }
//	case SCENE_EXTENTS:	{
//		if ( v == NULL )
//		{
//			s_err = QString( "There is no current view to present" );
//			return false;
//		}
//		//painter.translate( ext.left_, ext.bottom_ );

//		/* we are trying to fit */
//		REALV scl_f;
//		REALV w_scl = (REALV)rect.width() / ext.width();
//		REALV h_scl = (REALV)rect.height() / ext.height();
//		if ( w_scl > h_scl )
//		{
//			scl_f = w_scl;
//			/** @todo implement */
//		}
//		else
//		{
//			scl_f = h_scl;
//			/** @todo implement */
//		}
//		painter.scale( scl_f, scl_f );
//		QRectF	r;
//		spc->paint( painter, r );
//		break; }
//	case EXACT_RECT:

//	case PAGES:

//	default:
//		Q_ASSERT( false );
//	}


	painter.end();

	/** @todo new page */
	//		painter.drawRect( 0, 0, rect.width(), rect.height() );


	//		QRect rect = painter.viewport();
	////		QSize size = image.size();
	////		size.scale( rect.size(), Qt::KeepAspectRatio );
	////		painter.setViewport( rect.x(), rect.y(), size.width(), size.height() );
	////		painter.setWindow( image.rect() );
	//		painter.drawRect( 0, 0, rect.width(), rect.height() );
	//	}

	return true;
}
/* ========================================================================= */

/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
